﻿<%@ Page Title="客户设定" Language="C#" AutoEventWireup="true" CodeBehind="CustomerInfo.aspx.cs"
    Inherits="Page_CustomerInfo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>客户设定</title>
    <link rel="stylesheet" type="text/css" href="../../css/theme.css" />
    <link rel="stylesheet" type="text/css" href="../../css/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/icon.css" />
    <script src="../../js/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/Json.js" type="text/javascript"></script>
    <script src="../../js/Common.js" type="text/javascript"></script>
    
    <style type="text/css">
        .ftitle
        {
            font-size: 14px;
            font-weight: bold;
            padding: 5px 0;
            margin-bottom: 10px;
            border-bottom: 1px solid #ccc;
        }
        .fitem
        {
            margin-bottom: 5px;
        }
        .fitem label
        {
            display: inline-block;
            width: 60px; 
            text-align: right; 
            margin-right: 5px;
        }
        .searchBlock
        {
            padding-left: 20px;
        }
        .searchBlock span
        {
        }
        
        .searchTitle
        {
            padding-right: 2px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container" style="width: 100%; padding: 5px">
        <div id="toolbar" style="text-align: left; padding-bottom: 10px">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="pageJs.search()">搜索</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" id="add" onclick="pageJs.add()">
                新增</a> 
            <a href="javascript:void(0)" class="easyui-linkbutton" id="edit" onclick="pageJs.edit()">
                编辑</a> 
            <a href="javascript:void(0)" class="easyui-linkbutton" id="del" onclick="pageJs.del()">
                删除</a>
            <div style="padding-top: 5px">
                <span class="searchTitle">销售人员</span>
                    <input id="sSALESUSER" class="easyui-validatebox" />
                <span class="searchBlock"> 
                    <span class="searchTitle">客户名称</span>
                    <input style="width: 110px" id="sNAME" />
                </span>
            </div>
        </div>
        <div id="divTable" style="text-align: left">
            <div id="divGrid">
                <table id="dg" title="客户列表" style="height: 350px;" pagination="true" autorowheight="false"
                    pagesize="20" rownumbers="true" fitcolumns="false" singleselect="true">
                    <thead>
                        <tr>
                            <th width="100" field="CUSTOMER_ID">
                                客户代码
                            </th>
                            <th width="110" field="CUSTOMER_NAME">
                                客户名称
                            </th>
                            <th width="110" field="TELEPHONE">
                                手机号码
                            </th>
                            <th width="150" field="ADDRESS">
                                客户地址
                            </th>
                            <th width="100" field="SALES_USER_ID">
                                销售人员ID
                            </th>
                            <th width="100" field="SALES_USER_NAME">
                                销售人员
                            </th>
                            <th width="150" field="UPDATE_USER_ID">
                                异动人员编号
                            </th>
                            <th width="150" field="UPDATE_USER_NAME">
                                异动人员姓名
                            </th>
                            <th width="150" field="UPDATE_DATE">
                                异动日期
                            </th>
                        </tr>
                    </thead>
                </table>
                
                <div id="pnlDlg" style="display: none">
                    <div id="dlg" style="width: 400px; height: 350px; padding: 10px 20px; vertical-align: top"
                        title="add" data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
                        buttons="#dlg-buttons">
                        <div class="ftitle">
                            客户明细</div>
                        <div class="fitem">
                            <label>客户代码:</label>
                            <input name="CUSTOMER_ID" id="CUSTOMER_ID" class="easyui-validatebox" style="ime-mode: disabled;" />
                        </div>
                        <div class="fitem">
                            <label>客户名称:</label>
                            <input name="" id="CUSTOMER_NAME" />
                            <input id="CUSTOMER_KEY" type="hidden" />
                        </div>
                        <div class="fitem">
                            <label>手机号码:</label>
                            <input id="TELEPHONE" name="TELEPHONE" class="easyui-textbox" />
                        </div>
                        <div class="fitem">
                            <label>客户地址:</label>
                            <input id="ADDRESS" name="ADDRESS" class="easyui-textbox" />
                        </div>
                        <div class="fitem" id="div_sales">
                            <label>销售人员:</label>
                            <input id="dllSalesUser" class="easyui-validatebox" />
                        </div>
                    </div>
                    <div id="dlg-buttons">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="pageJs.doSave()">
                            保存</a> 
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
                            onclick="javascript:$('#dlg').dialog('close')">
                            取消</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </form>
    
    <script type="text/javascript">
        var pageJs = {
            url: "",
            resize: function(h, w) {
                $('#container').height(h).width(w - 10);
                var contentH = h - $("#toolbar").outerHeight() - 10;
                if (contentH < 100) contentH = 100;
                $("#dg").datagrid("resize", { height: contentH });
            },
            validate: function() {
                var BasicCode = $.trim($("#CUSTOMER_ID").val());
                var BasicName = $.trim($("#CUSTOMER_NAME").val());
                var SalesUser = $.trim($('#dllSalesUser').combogrid('getText'));

                if (SalesUser == "")
                    return "销售人员是必须项！";

                if (BasicCode == "")
                    return "客户代码为必须项！";

                if (BasicName.length > 50)
                    return "客户名称最长50字符！";

                if (BasicCode.length > 50)
                    return "客户代码最长25字符！";

                return "";

            },
            doSave: function() {
                var valMsg = this.validate();
                if (valMsg != "") {
                    BasicJs.showMessage(valMsg);
                    return;
                }
                var parm = { CUSTOMER_ID: $.trim($("#CUSTOMER_ID").val()), CUSTOMER_NAME: $.trim($("#CUSTOMER_NAME").val()),
                    SALES_USER_KEY: $('#dllSalesUser').combogrid("getValue"), CUSTOMER_KEY: $("#CUSTOMER_KEY").val(),
                    TELEPHONE: $('#TELEPHONE').val(), ADDRESS: $('#ADDRESS').val()
                };
                var action = "add";
                if ($("#CUSTOMER_KEY").val() != "0") action = "edit";
                $.post('../../ashx/Basic/hCustomerInfo.ashx?action=' + action, { data: JSON.stringify(parm) },function(result) {                
                        if (result.Success) {
                            if ($("#CUSTOMER_KEY").val() != "0") {
                                //edit close dialog
                                var callback = function() { $('#dlg').dialog('close'); };
                                BasicJs.showMessage("保存成功！", true, callback);
                            }
                            else {//add
                                BasicJs.showMessage("保存成功！", true);
                                pageJs.initAdd();
                                setTimeout('$("#CUSTOMER_NAME").focus()', 100);
                            }
                            $('#dg').datagrid('reload');
                        }
                        else {
                            //error
                            if (result.Message == "此客户已被删除，是否还原？") {
                                $.messager.confirm('还原客户', result.Message, function(r) {
                                    if (r) {
                                        $.post(pageJs.url + "?action=revert", { id: $.trim($("#CUSTOMER_ID").val()) }, function(result) {
                                            if (result.Success) {
                                                $('#dg').datagrid('reload');    // reload the user data
                                                pageJs.initAdd();
                                                BasicJs.showMessage("还原成功！", true);
                                            } else {
                                                if (result.Message) BasicJs.showMessage(result.Message);
                                            }
                                        }, 'json');
                                    }
                                });
                            }
                            else BasicJs.showMessage(result.Message);
                        }
                        //close dialog

                    }, 'json'
             );

            },

            initAdd: function() {
                $('#dllSalesUser').combogrid("setValue", "");
                $("#CUSTOMER_KEY").val("0");
                $("#CUSTOMER_ID").val("").removeAttr("disabled").focus();
                $("#CUSTOMER_NAME").val("");
                $('#TELEPHONE').val("");
                $('#ADDRESS').val("");
            },
            add: function() {
                this.initAdd();
                var pos = BasicJs.getIFrameCenter("#dlg");
                $("#pnlDlg").show();
                var position = BasicJs.autoDialogSize(350, 400);
                $('#dlg').dialog(pos).dialog({
                    left: position.Left,
                    top: position.Top,
                    height: position.Height,
                    width: position.Width,
                    "onOpen": function() {
                        setTimeout(' $("#CUSTOMER_ID").focus();', 100);
                    }
                }).dialog('open').dialog('setTitle', '新增客户');
            },

            edit: function() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $("#CUSTOMER_KEY").val(row.CUSTOMER_KEY);
                    $("#CUSTOMER_ID").val(row.CUSTOMER_ID).attr("disabled", "disabled");
                    $("#CUSTOMER_NAME").val(row.CUSTOMER_NAME);
                    $("#TELEPHONE").val(row.TELEPHONE);
                    $('#ADDRESS').val(row.ADDRESS);
                    $('#dllSalesUser').combogrid("setValue", row.SALES_USER_KEY);
                    var pos = BasicJs.getCenter("#dlg");
                    $("#pnlDlg").show();
                    var position = BasicJs.autoDialogSize(350, 400);
                    $('#dlg').dialog(pos).dialog({
                        left: position.Left,
                        top: position.Top,
                        height: position.Height,
                        width: position.Width,
                        "onOpen": function() {
                            setTimeout(' $("#CUSTOMER_NAME").focus();', 100);
                        }
                    }).dialog('open').dialog('setTitle', '编辑客户');
                }
                else { BasicJs.showMessage("未选中记录！"); }

            },

            del: function() {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('删除客户', '您确定要刪除选中的客户吗?', function(r) {
                        if (r) {
                            $.post(pageJs.url + "?action=del",
                                { data: JSON.stringify(row) },
                                function(result) {
                                    if (result.Success) {
                                        $('#dg').datagrid('reload');    // reload 
                                        BasicJs.showMessage("刪除成功！", true);
                                    }
                                    else {
                                        if (result.Message) BasicJs.showMessage(result.Message);
                                    }
                                }, 'json');
                        }
                    });
                }
                else { BasicJs.showMessage("未选中记录！"); }
            },
            search: function() {
                var arrSalesUser = $('#sSALESUSER').combogrid("grid").datagrid("getSelections");
                var row_i = 0;
                var _salesUser = "";
                while (arrSalesUser[row_i]) {
                    _salesUser = _salesUser + arrSalesUser[row_i].USER_KEY + ",";
                    row_i++;
                }
                $('#dg').datagrid('load',
                 {
                     sSALESUSER: _salesUser,
                     sNAME: $.trim($('#sNAME').val())
                 }
            );

            },

            dataFilter: function(data) {
                return data;
            }

        }

        $(function() {
            //控件显示
            //$('#TELEPHONE').textbox();

            //授权
            var funKey = window.name.substring(3);
            BasicJs.authorize("#add", funKey, "ADD");
            BasicJs.authorize("#edit", funKey, "EDIT");
            BasicJs.authorize("#del", funKey, "DELETE");

            //销售人员下拉
            $('#dllSalesUser').combogrid({
                delay: 500,
                panelWidth: 250,
                panelHeight: 300,
                //mode: 'local',
                idField: 'USER_KEY',
                textField: 'USER_NAME',
                 url: "../../ashx/System/hUserGroup.ashx?action=getuser",
                //data: window.top.cacheData.Factories,
                fit: true,
                editable: false,
                multiple: false,
                pagination: false, //是否分页
                columns: [[

                        { field: 'USER_KEY', title: '', width: 100, hidden: true },
                        { field: 'USER_ID', title: '销售人员代码', width: 100, sortable: false },
                        { field: 'USER_NAME', title: '销售人员名称', width: 100, sortable: false }
                    ]]
            });
            $('#sSALESUSER').combogrid({
                delay: 500,
                panelWidth: 250,
                panelHeight: 300,
                //mode: 'local',
                idField: 'USER_KEY',
                textField: 'USER_NAME',
                 url: "../../ashx/System/hUserGroup.ashx?action=getuser",
                //data: window.top.cacheData.Factories,
                fit: true,
                editable: false,
                multiple: true,
                pagination: false, //是否分页
                columns: [[
                        { field: 'ck', checkbox: true },
                        { field: 'USER_KEY', title: '', width: 100, hidden: true },
                        { field: 'USER_ID', title: '销售人员代码', width: 100, sortable: false },
                        { field: 'USER_NAME', title: '销售人员名称', width: 100, sortable: false }
                    ]]
            });
            $(".easyui-numberbox").removeClass("validatebox-text");
            $("#toolbar input").removeClass("validatebox-text");


            $('#dg').datagrid({
                url: "../../ashx/Basic/hCustomerInfo.ashx?action=get",
                loadMsg: "正在加载数据，请稍后...",
                onLoadSuccess: function(data) {
                    if (data.total > 0) {
                        $('#dg').datagrid("selectRow", 0);
                    }
                },
                onSelect: function(rowIndex, rowData) {
                    //群组对应的菜单权限
                    $('#divtreebtn').hide();
                    if (rowData) {
                    }
                }
            });
            pageJs.resize($(window).height(), $(window).width());
            pageJs.url = '../../ashx/Basic/hCustomerInfo.ashx';
            BasicJs.bindEvent();
        });  
    </script>
</body>
</html>
